using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using UnityEngine;


namespace IQIGame.Onigao.GamePlay
{
    public class UITestWindow : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UITestWindow),
            parentPath = "NoPack",
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UITestWindow
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExImage image { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btn_Blur { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btn_PlayVideo { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btn_StopVideo { protected set; get; }
            public UnityEngine.RectTransform[] array { protected set; get; }
            public UnityEngine.UI.ExLoopVerticalScrollRect verticalLoopScroll { protected set; get; }
            public UnityEngine.GameObject listItem { protected set; get; }
            public IQIGame.Onigao.Framework.ExRawImage img_VideoTest { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btn_Live2D { protected set; get; }
            public IQIGame.Onigao.Framework.ExCanvas live2DBack { protected set; get; }
            public IQIGame.Onigao.Framework.ExCanvas live2D { protected set; get; }
            public IQIGame.Onigao.Framework.ExCanvas live2DFront { protected set; get; }
            public UnityEngine.Transform live2DParent { protected set; get; }
            protected virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("image", out var __tbv0);
                this.image = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btn_Blur", out var __tbv1);
                this.btn_Blur = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btn_PlayVideo", out var __tbv2);
                this.btn_PlayVideo = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btn_StopVideo", out var __tbv3);
                this.btn_StopVideo = __tbv3;
                var __tbv4 = __binding.GetVariableByName("array");
                this.array = new UnityEngine.RectTransform[__tbv4?.arrayValueCount ?? 0];
                for (int __index = 0; __index < this.array.Length; __index++)
                {
                    this.array[__index] = __tbv4.variableArray.GetValue<UnityEngine.RectTransform>(__index);
                }
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("verticalLoopScroll", out var __tbv5);
                this.verticalLoopScroll = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("listItem", out var __tbv6);
                this.listItem = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExRawImage>("img_VideoTest", out var __tbv7);
                this.img_VideoTest = __tbv7;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btn_Live2D", out var __tbv8);
                this.btn_Live2D = __tbv8;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExCanvas>("live2DBack", out var __tbv9);
                this.live2DBack = __tbv9;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExCanvas>("live2D", out var __tbv10);
                this.live2D = __tbv10;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExCanvas>("live2DFront", out var __tbv11);
                this.live2DFront = __tbv11;
                __binding.TryGetVariableValue<UnityEngine.Transform>("live2DParent", out var __tbv12);
                this.live2DParent = __tbv12;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        /// <summary>
        /// 可以自定义修改的
        /// </summary>
        protected partial class UIB_UITestWindow
        {
            public virtual void StartBinding(GameObject __go)
            {
                var binding = __go.GetComponent<ObjectBinding>();
                if (binding != null)
                {
                    this.InitBinding(binding);
                }
            }
        }
        protected UIB_UITestWindow ui { get; set; }

        // 循环列表示例，针对Item很多的情况。
        private LoopScrollAdapter<UINTestScrollItem> _loopScrollAdapter;

        // 普通列表示例，针对Item不多的情况。
        private UIItemPool<UINTestScrollItem> _listItemPool;

        protected override void BeforeInit()
        {
            ui = new UIB_UITestWindow();
            ui.StartBinding(gameObject);
        }

        protected override void OnInit()
        {
            this.ui.btn_Blur.AddClickEvent(() => ShowBlurTest().Forget());
            this.ui.btn_PlayVideo.AddClickEvent(() => { PlayVideoTest().Forget(); });
            this.ui.btn_StopVideo.AddClickEvent(StopVideoTest);
            this.ui.btn_Live2D.AddClickEvent(CreateLive2D);

            TestLoopScrollView();

            TestNormalListView();
        }

        protected override void OnShow(UIBaseData data)
        {
            this.ui.live2DBack.UpdateSortingOrderByParent();
            this.ui.live2D.UpdateSortingOrderByParent();
            this.ui.live2DFront.UpdateSortingOrderByParent();
        }

        protected override void OnHide()
        {

        }

        #region 循环列表使用示例
        private void TestLoopScrollView()
        {
            // 使用循环列表适配器初始化(更上层的封装，业务写起来更简单)，也可以自己使用原始的对象写(需要多写些代码)。
            _loopScrollAdapter = new LoopScrollAdapter<UINTestScrollItem>(this.ui.verticalLoopScroll, this).BindUnitChangedCall(OnScrollItemChanged);
            // 根据数据来设置Item数量
            _loopScrollAdapter.RefillCells(100);
        }

        private void OnScrollItemChanged(UINTestScrollItem item, int index)
        {
            // 实际需要根据index获取数据，然后刷新用数据刷新Item；
            // 例如 var data = this.dataList[index]; item.RefreshByData(data);
            item.RefreshScrollItem(index);
        }

        #endregion

        #region 普通列表使用示例

        public void TestNormalListView()
        {
            // 初始化对象
            _listItemPool = this.InitPool<UINTestScrollItem>(this.ui.listItem);

            // 刷新列表
            RefreshNormalListView();
        }

        public void RefreshNormalListView()
        {
            // 实际需要根据数据来刷新
            _listItemPool.HideAll();
            for (int i = 0; i < 20; i++)
            {
                var item = _listItemPool.GetOne();
                item.RefreshScrollItem(i);
            }
        }

        #endregion

        /// <summary>
        /// UI背景模糊示例
        /// </summary>
        /// <returns></returns>
        async UniTask ShowBlurTest()
        {
            LogGame.Log("LoginWindow Button Clicked");
            await ManagerCenter.UI.ShowWindowAsync<UIBlurTest>();
        }

        private VideoPlayerForUI videoPlayer;

        /// <summary>
        /// 播放视频示例
        /// </summary>
        /// <returns></returns>
        async UniTask PlayVideoTest()
        {
            if (videoPlayer != null)
            {
                return;
            }

            // 从对象池中拿取VideoPlayer
            videoPlayer = ManagerCenter.Video.GetVideoPlayerForUI();
            this.ui.img_VideoTest.gameObject.SetActive(true);
            videoPlayer.SetVideoTarget(this.ui.img_VideoTest);

            await videoPlayer.Play("video_test");

            StopVideoTest();

            Debug.LogError("视频播放完成！");

        }

        void StopVideoTest()
        {
            if (videoPlayer == null)
            {
                return;
            }
            // 引用置空前，必须调用这个,否则内存泄露。StopVideo会返回对象池
            videoPlayer.StopVideo();
            videoPlayer = null;
            this.ui.img_VideoTest.gameObject.SetActive(false);
        }

        private int l2dID = -1;
        void CreateLive2D()
        {
            if (this.l2dID <= 0)
            {
                this.l2dID = Live2DManager.Instance.Create("Live2DTest", this.ui.live2DParent, this.ui.live2D.currCav.sortingLayerName, this.ui.live2D.currCav.sortingOrder);
            }
            else
            {
                Live2DManager.Instance.Remove(this.l2dID);
                this.l2dID = -1;
            }
        }

        protected override void OnDispose()
        {

        }
    }
}
